package com.huan.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ScopeController {
    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request){
        request.setAttribute("testRequestScope","hello,servletAPI");
        return "success";
    }
    @RequestMapping("/testModeAndView")
    public ModelAndView testModeAndView(){
        ModelAndView mav = new ModelAndView();
        //处理模型数据，即向请求域request共享数据
        mav.addObject("testRequestScope","hello,ModeAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;//相当于return "success"
    }
    @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("testRequestScope","hello,model");
        System.out.println(model.getClass().getName());
        return "success";
    }
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","hello,model");
        System.out.println(map.getClass().getName());
        return "success";
    }
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap map){
        map.addAttribute("testRequestScope","hello,ModelMap");
        System.out.println(map.getClass().getName());
        return "success";
    }
    @RequestMapping("/testSession")
    public String teseSession(HttpSession session){
        session.setAttribute("testSessionScope","hello,session");
        System.out.println(session);
        return "success";
    }
    @RequestMapping("testApplication")
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope","hello,Application");
        return "success";
    }
}

